<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\AdConfig;
use Common\Models\AdPosition;
use Common\Models\Carousel;
use Common\Models\Func;
use Common\Models\Hot;
use Common\Models\JdPdd;
use Common\Models\Material;
use Common\Models\Menu;
use Common\Models\SearchBar;
use Common\Models\Ad;
use Common\Models\SelfCenterView;
use Common\Models\Templates;
use Common\Models\ViewHomeXcxConfig;
use Library\ConstKey\SystemKey;
use Library\Tools\RedisTools;

class HomeController extends ApiController
{
	public function initialize()
	{
		parent::initialize();
	}
	//首页结构
	public function IndexAction(){
        $redis_key = Common::redisKey($this->params);
        $data = (new RedisTools())->get($redis_key);
        if(!$data && !SystemKey::ISREDIS) {
            $templates = new Templates();
            //模板
            $version = null;
            if (isset($this->params['version'])) {
                $version = $this->params['version'];
            }
            $templates_one = $templates->getDefTemplates($this->params['source'], $version);
            $data = [];
            $search_bar = new SearchBar();
            $material = new Material();
            //搜索框
            $search_bar_one = $search_bar->FindByid($templates_one->search_id)->toArray();
            $search_bar_one['right'] = isset($material->getListByIds($search_bar_one['right_id'], $this->user_info)[0]) ? $material->getListByIds($search_bar_one['right_id'], $this->user_info)[0] : (object)null;
            unset($search_bar_one['right_id']);

            $search_bar_one['left'] = isset($material->getListByIds($search_bar_one['left_id'], $this->user_info)[0]) ? $material->getListByIds($search_bar_one['left_id'], $this->user_info)[0] : (object)null;

            unset($search_bar_one['left_id']);
            $encode = mb_detect_encoding($search_bar_one['name']);
            //轮播
            $data['search_bar'] = $search_bar_one;
            $carousel = new Carousel();
            $carousel_one = $carousel->FindByid($templates_one->carousel_id);
            $carousel_list = $material->getListByIds($carousel_one->ids, $this->user_info, 1);
            $data['carousel']['data'] = $carousel_list;
            $data['carousel']['height'] = $carousel_one ? $carousel_one->height : 0;

            $menu = new Menu();
            if ($templates_one->menu_id) {
                $menu_one = $menu->FindByid($templates_one->menu_id);
                $data['menu'] = $material->getListByIds($menu_one->ids, $this->user_info);
                if (isset($this->params['app'])) {
                    $data['menu'] = array_chunk($data['menu'], 10);
                }
            } else {
                $data['menu'] = [];
            }
            //广告
            $ad_config = new AdConfig();
            $ad = new Ad();
            $data['ad'] = $data['ad1'] = $data['ad2'] = [];
            if ($templates_one->ad_id) {
                $ad_config_one = $ad_config->FindByid($templates_one->ad_id);
                $ad_list = $ad->getListByIds($ad_config_one->ids, $this->user_info);
                $data['ad'] = $ad_list;
            }


            if ($templates_one->ad1_id) {
                $ad_config_one = $ad_config->FindByid($templates_one->ad1_id);
                $ad_list = $ad->getListByIds($ad_config_one->ids, $this->user_info);
                $data['ad1'] = $ad_list;
            }

            if ($templates_one->ad2_id) {
                $ad_config_one = $ad_config->FindByid($templates_one->ad2_id);
                $ad_list = $ad->getListByIds($ad_config_one->ids, $this->user_info);
                $data['ad2'] = $ad_list;
            }

            $data['background_image'] = $templates_one->background_image_id ? $templates_one->background_image_id : '';
            $data['carousel_background_image'] = $templates_one->carousel_background_image_id ? $templates_one->carousel_background_image_id : '';
            $data['menu_background_image'] = $templates_one->menu_background_image_id ? $templates_one->menu_background_image_id : '';
            $data['font_color'] = $templates_one->font_color ? $templates_one->font_color : '#FFFFFF';
            $data['current_color'] = $templates_one->current_color ? $templates_one->current_color : '#000000';

            //热点
            $hot = new Hot();
            $hot_id = $hot->find_by_id(1);
            $data['hot'] = $material->getListByIds($hot_id['ids'], $this->user_info);
            $hot_id2 = $hot->find_by_id(2);
            $data['hot_tt'] = $material->getListByIds($hot_id2['ids'], $this->user_info);

            if ($templates_one->func) {
                $func = new Func();
                $func_one = $func->FindByid($templates_one->func);
                $data['func'] = $material->getListByIds($func_one->ids, $this->user_info);
            } else {
                $data['func'] = [];
            }
            (new RedisTools())->set($redis_key,$data);
        }
        $this->ok($data);
	}

	//京东结构
	public function jpAction(){
		$type=$this->params['type'];
        $redis_key = Common::redisKey($this->params);
        $res = (new RedisTools())->get($redis_key);
        if(!$res && !SystemKey::ISREDIS) {
            $jd = new JdPdd();
            $material = new Material();
            $data['carousel'] = [];
            $data['ad'] = [];
            $data['top_ad'] = [];
            $data['background'] = (object)null;
            $res = $jd->getConfig($type);
            if (!$res) {
                $this->ok($data);
            }

            if ($res->carousel_id) {
                $carousel = new Carousel();
                $carousel_one = $carousel->FindByid($res->carousel_id);
                $data['carousel'] = $material->getListByIds($carousel_one->ids, $this->user_info, 1);
            } else {
                $data['carousel'] = [];
            }

            if ($res->ad_id) {
                $ad_config = new AdConfig();
                //广告
                $ad_config_one = $ad_config->FindByid($res->ad_id);
                $ad = new Ad();
                $ad_list = $ad->getListByIds($ad_config_one->ids, $this->user_info);
                $data['ad'] = $ad_list;
            } else {
                $data['ad'] = [];
            }

            if ($res->top_ad_id) {
                $top_ad_config = new AdConfig();
                //广告
                $top_ad_config_one = $top_ad_config->FindByid($res->top_ad_id);
                $top_ad = new Ad();
                $top_ad_list = $top_ad->getListByIds($top_ad_config_one->ids, $this->user_info);
                $data['top_ad'] = $top_ad_list;
            } else {
                $data['top_ad'] = [];
            }

            if ($res->menu_id) {
                $menu = new Menu();
                $menu_one = $menu->FindByid($res->menu_id);
                $data['menu'] = $material->getListByIds($menu_one->ids, $this->user_info);
                $data['menu'] = $data['menu'] ? array_chunk($data['menu'], 10) : [];
            } else {
                $data['menu'] = [];
            }
            if ($res->background_id) {
                $background = $material->getListByIds($res->background_id, $this->user_info, 1);
                if ($background) {
                    $data['background'] = $background[0];
                } else {
                    $data['background'] = (object)null;
                }
            } else {
                $data['background'] = (object)null;
            }
            (new RedisTools())->set($redis_key,$data);
        }
		$this->ok($data);
	}

	//小程序首页结构
    public function xcx_homeAction(){
        $redis_key = Common::redisKey($this->params);
        $data = (new RedisTools())->get($redis_key);
        if(!$data && !SystemKey::ISREDIS){
            $ViewHomeXcxConfigDb = new ViewHomeXcxConfig();
            $res =$ViewHomeXcxConfigDb->get_xcx("home");
            $material = new Material();
            if (isset($res->carousel_id) && $res->carousel_id) {
                $carousel = new Carousel();
                $carousel_one = $carousel->FindByid($res->carousel_id);
                $data['carousel'] = $carousel_one && $carousel_one->ids ? $material->getListByIds($carousel_one->ids, $this->user_info, 1) : [];
            } else {
                $data['carousel'] = [];
            }
            if (isset($res->ad_id) && $res->ad_id) {
                $ad_config = new AdConfig();
                //广告
                $ad_config_one = $ad_config->FindByid($res->ad_id);
                $ad = new Ad();
                $ad_list = $ad_config_one && $ad_config_one->ids ? $ad->getListByIds($ad_config_one->ids, $this->user_info) : [];
                $data['ad'] = $ad_list;
            } else {
                $data['ad'] = [];
            }
            if (isset($res->menu_id) && $res->menu_id) {
                $menu = new Menu();
                $menu_one = $menu->FindByid($res->menu_id);
                $data['menu'][] = $menu_one && $menu_one->ids ? $material->getListByIds($menu_one->ids, $this->user_info) : [];
            } else {
                $data['menu'] = [];
            }
            (new RedisTools())->set($redis_key,$data);
        }
        $this->ok($data);
    }

    //小程序个人中心结构
    public function xcx_centerAction(){
        $redis_key = Common::redisKey($this->params);
        $data = (new RedisTools())->get($redis_key);
        if(!$data && !SystemKey::ISREDIS){
            $ViewHomeXcxConfigDb = new ViewHomeXcxConfig();
            $res =$ViewHomeXcxConfigDb->get_xcx("center");
            $material = new Material();
            if (isset($res->carousel_id) && $res->carousel_id) {
                $carousel = new Carousel();
                $carousel_one = $carousel->FindByid($res->carousel_id);
                $data['carousel'] = $carousel_one && $carousel_one->ids ? $material->getListByIds($carousel_one->ids, $this->user_info, 1) : [];
            } else {
                $data['carousel'] = [];
            }
            if (isset($res->ad_id) && $res->ad_id) {
                $ad_config = new AdConfig();
                //广告
                $ad_config_one = $ad_config->FindByid($res->ad_id);
                $ad = new Ad();
                $ad_list = $ad_config_one && $ad_config_one->ids ? $ad->getListByIds($ad_config_one->ids, $this->user_info) : [];
                $data['ad'] = $ad_list;
            } else {
                $data['ad'] = [];
            }
            if (isset($res->menu_id) && $res->menu_id) {
                $menu = new Menu();
                $menu_one = $menu->FindByid($res->menu_id);
                $menu_arr = $menu_one && $menu_one->ids ? $material->getListByIds($menu_one->ids, $this->user_info) : [];
                $data['menu'] = array_chunk($menu_arr,4);
            } else {
                $data['menu'] = [];
            }
            (new RedisTools())->set($redis_key,$data);
        }
        $this->ok($data);
    }

	//个人中心
	public function self_centerAction(){
        $redis_key = Common::redisKey($this->params);
        $data = (new RedisTools())->get($redis_key);
        if(!$data  && !SystemKey::ISREDIS) {
            $self_center_view = new SelfCenterView();
            $res = $self_center_view->getConfig($this->params);
            $ad_config = new AdConfig();
            $material = new Material();
            //广告
            $ad = new Ad();
            $data['ad1'] = $data['ad2'] = $data['menu'] = $data['menu1'] = $data['menu2'] = [];
            if ($res->ad1_id) {
                $ad_config_one = $ad_config->FindByid($res->ad1_id);
                $ad_list = $ad->getListByIds($ad_config_one->ids, $this->user_info);
                $data['ad1'] = $ad_list;
            }
            $menu = new Menu();
            if ($res->center_id) {
                $menu_one = $menu->FindByid($res->center_id);
                $data['center'] = $material->getListByIds($menu_one->ids, $this->user_info);
            }
            if ($res->menu) {
                $menu_one = $menu->FindByid($res->menu);
                $data['menu'] = $material->getListByIds($menu_one->ids, $this->user_info);
            }
            if ($res->menu_id) {
                $menu_one = $menu->FindByid($res->menu_id);
                $data['menu1'] = $material->getListByIds($menu_one->ids, $this->user_info);
            }
            if ($res->menu1_id) {
                $menu_one = $menu->FindByid($res->menu1_id);
                $data['menu2'] = $material->getListByIds($menu_one->ids, $this->user_info);
            }
            if ($res->ad2_id) {
                $ad_config_one = $ad_config->FindById($res->ad2_id);
                if (isset($ad_config_one->ids) && !empty($ad_config_one->ids)) {
                    $ad_list = $ad->getListByIds($ad_config_one->ids, $this->user_info);
                } else {
                    $ad_list = [];
                }
                $data['ad2'] = $ad_list;
            }
            $data['background_color'] = $res->background_color;
            $data['background_img'] = !empty($res->background_id) ? $material->getListByIds($res->background_id, $this->user_info)[0]['path'] : '';
            $data['background1_img'] = !empty($res->background1_id) ? $material->getListByIds($res->background1_id, $this->user_info)[0]['path'] : '';
            (new RedisTools())->set($redis_key,$data);
        }
        $this->ok($data);
	}

    //首页弹出广告
    public function home_tan_adAction(){
        $adPosition = new AdPosition();
        $home_ad = $adPosition->home_tan_ad();
        if(!$home_ad){
            $this->ok([]);
        }
        $material      = new Material();
        if($home_ad->ids){
            $material_list = $material->getListByIds($home_ad->ids,$this->user_info);
        }else{
            $material_list = [];
        }
        $result=[];
        if(time()<= strtotime($home_ad->expire_at)){
            if($home_ad->num==0){
                $adPositionUser = new AdPositionUser();
                $user_data = $adPositionUser->find_by_user_id_ap_id($this->params['user_id'],$home_ad->id);
                if(!$user_data){
                    $adPositionuser = new AdPositionUser();
                    $adPositionuser->user_id=$this->params['user_id'];
                    $adPositionuser->ap_id=$home_ad->id;
                    $adPositionuser->save();
                    $result['list']=$material_list;
                }else{
                    $result['list']=[];
                }
            }else{
                $result['list']=$material_list;
            }
        }
        $this->ok($result);
    }



}
